home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / mpeg / 24bit.c next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  229 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include <stdlib.h>
  22. #include "video.h"
  23. #include "dither.h"
  24. #include "proto.h"
  25.  
  26.  
  27. /*
  28.  * We'll define the "ConvertColor" macro here to do fixed point arithmetic
  29.  * that'll convert from YCrCb to RGB using:
  30.  *    R = L + 1.40200*Cr;
  31.  *    G = L - 0.34414*Cb - 0.71414*Cr
  32.  *    B = L + 1.77200*Cb;
  33.  *
  34.  * We'll use fixed point by adding two extra bits after the decimal.
  35.  */
  36.  
  37. #define BITS    8
  38. #define ONE     ((int) 1)
  39. #define CONST_SCALE    (ONE << BITS)
  40. #define ROUND_FACTOR    (ONE << (BITS-1))
  41.  
  42. /* Macro to convert integer to fixed. */
  43. #define UP(x)    (((int)(x)) << BITS)
  44.  
  45. /* Macro to convert fixed to integer (with rounding). */
  46. #define DOWN(x)    (((x) + ROUND_FACTOR) >> BITS)
  47.  
  48. /* Macro to convert a float to a fixed */
  49. #define FIX(x)  ((int) ((x)*CONST_SCALE + 0.5))
  50.  
  51. #define CLAMP(ll,x,ul)    ( ((x)<(ll)) ?(ll):( ((x)>(ul)) ?(ul):(x)))
  52.  
  53. static int *Cb_r_tab, *Cr_g_tab, *Cb_g_tab, *Cr_b_tab;
  54.  
  55. /*
  56.  *--------------------------------------------------------------
  57.  *
  58.  * InitColorDither --
  59.  *
  60.  *    To get rid of the multiply and other conversions in color
  61.  *    dither, we use a lookup table.
  62.  *
  63.  * Results:
  64.  *    None.
  65.  *
  66.  * Side effects:
  67.  *    The lookup tables are initialized.
  68.  *
  69.  *--------------------------------------------------------------
  70.  */
  71.  
  72. void
  73. InitColorDither()
  74. {
  75.     int CR, CB, i;
  76.  
  77.     Cr_b_tab = (int *)malloc(256*sizeof(int));
  78.     Cr_g_tab = (int *)malloc(256*sizeof(int));
  79.     Cb_g_tab = (int *)malloc(256*sizeof(int));
  80.     Cb_r_tab = (int *)malloc(256*sizeof(int));
  81.  
  82.     for (i=0; i<256; i++) {
  83.     CB = CR = i;
  84.  
  85.     CB -= 128; CR -= 128;
  86.  
  87.     Cb_r_tab[i] = FIX(1.40200) * CB;
  88.     Cr_g_tab[i] = -FIX(0.34414) * CR;
  89.     Cb_g_tab[i] = -FIX(0.71414) * CB;   
  90.     Cr_b_tab[i] = FIX(1.77200) * CR;
  91.     }
  92. }
  93.  
  94.  
  95. /*
  96.  *--------------------------------------------------------------
  97.  *
  98.  * ColorDitherImage --
  99.  *
  100.  *    Converts image into 24 bit color.
  101.  *
  102.  * Results:
  103.  *    None.
  104.  *
  105.  * Side effects:
  106.  *    None.
  107.  *
  108.  *--------------------------------------------------------------
  109.  */
  110.  
  111.  
  112. void
  113. ColorDitherImage(lum, cr, cb, out, rows, cols)
  114.   unsigned char *lum;
  115.   unsigned char *cr;
  116.   unsigned char *cb;
  117.   unsigned char *out;
  118.   int cols, rows;
  119.  
  120. {
  121.     int L, CR, CB;
  122.     unsigned int *row1, *row2, *head;
  123.     unsigned char *lum2;
  124.     int x, y;
  125.     unsigned int r, b, g;
  126.     int cb_r;
  127.     int cr_g;
  128.     int cb_g;
  129.     int cr_b;
  130.         int R, G, B;
  131.  
  132. #if 0
  133.     row1 = (unsigned int *)out;
  134.     row2 = row1 + cols;
  135. #else
  136.     head  = (unsigned int *) out;
  137. #endif
  138.     lum2 = lum + cols;
  139.     for (y=0; y<rows; y+=2) {
  140.     row1 = head + cols * ( rows - 1 - y);
  141.     row2 = row1 - cols;
  142.     for (x=0; x<cols; x+=2) {
  143.  
  144.         CR = *cr++;
  145.         CB = *cb++;
  146.         cb_r = Cb_r_tab[CB];
  147.         cr_g = Cr_g_tab[CR];
  148.         cb_g = Cb_g_tab[CB];
  149.         cr_b = Cr_b_tab[CR];
  150.  
  151.         L = *lum++;
  152.         L = UP(L);
  153.         R = L + cb_r;
  154.         G = L + cr_g + cb_g;
  155.         B = L + cr_b;
  156.  
  157. #ifndef RS6000
  158.         r = CLAMP(0,R,UP(255)) >> BITS;
  159.         g = CLAMP(0,G,UP(255)) & 0xff00;
  160.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  161. #else
  162.         b = CLAMP(0,B,UP(255)) >> BITS;
  163.         g = CLAMP(0,G,UP(255)) & 0xff00;
  164.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  165. #endif
  166.         *row1++ = r | g | b;
  167.         L = *lum++;
  168.         L = UP(L);
  169.         R = L + cb_r;
  170.         G = L + cr_g + cb_g;
  171.         B = L + cr_b;
  172. #ifndef RS6000
  173.         r = CLAMP(0,R,UP(255)) >> BITS;
  174.         g = CLAMP(0,G,UP(255)) & 0xff00;
  175.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  176. #else
  177.         b = CLAMP(0,B,UP(255)) >> BITS;
  178.         g = CLAMP(0,G,UP(255)) & 0xff00;
  179.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  180. #endif
  181.         *row1++ = r | g | b;
  182.  
  183.         /*
  184.          * Now, do second row.
  185.          */
  186.         L = *lum2++;
  187.         L = UP(L);
  188.         R = L + cb_r;
  189.         G = L + cr_g + cb_g;
  190.         B = L + cr_b;
  191. #ifndef RS6000
  192.         r = CLAMP(0,R,UP(255)) >> BITS;
  193.         g = CLAMP(0,G,UP(255)) & 0xff00;
  194.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  195. #else
  196.         b = CLAMP(0,B,UP(255)) >> BITS;
  197.         g = CLAMP(0,G,UP(255)) & 0xff00;
  198.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  199. #endif
  200.         *row2++ = r | g | b;
  201.  
  202.         L = *lum2++;
  203.         L = UP(L);
  204.         R = L + cb_r;
  205.         G = L + cr_g + cb_g;
  206.         B = L + cr_b;
  207. #ifndef RS6000
  208.         r = CLAMP(0,R,UP(255)) >> BITS;
  209.         g = CLAMP(0,G,UP(255)) & 0xff00;
  210.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  211. #else
  212.         b = CLAMP(0,B,UP(255)) >> BITS;
  213.         g = CLAMP(0,G,UP(255)) & 0xff00;
  214.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  215. #endif
  216.         *row2++ = r | g | b;
  217.     }
  218.     lum += cols;
  219.     lum2 += cols;
  220. #if 0
  221.     row1 += cols;
  222.     row2 += cols;
  223. #endif
  224.     }
  225. }
  226.  
  227.  
  228.  
  229.